// pages/roomOnline/roomOnline.js
let md5 = require("../../utils/md5.js")
let axios = require("../../utils/util.js")
const app = getApp()
Page({

  /**
   * 页面的初始数据
   */
  data: {
    page: 1,
    roomId: "",
    onlineList: "",
    request: false,
  },
  //抱人上麦
  holdWheat(data) {
    if (this.data.request) {
      let timestamp = (Date.parse(new Date()) / 1000).toString();
      let arys = {
        roomId: this.data.roomId,
        userId: app.globalData.user_id.toString,
        token: app.globalData.userToken,
        memberId: data.currentTarget.dataset.memberid,
        time: timestamp,
      };
      let _token = axios.encryption(arys);
      let params = {
        roomId: this.data.roomId,
        userId: app.globalData.user_id,
        token: app.globalData.userToken,
        time: timestamp,
        memberId: data.currentTarget.dataset.memberid,
        sign: md5.hexMD5(_token),
      };
      axios.axios({
        url: "/chat/holdWheat",
        data: params
      }).then((res) => {
        if (res.data.status == 0) {
          wx.showToast({
            title: res.data.text,
            icon: 'none',
            duration: 2000 //持续的时间
          })
          setTimeout(function () {
            wx.navigateBack()
          }, 2000)
        } else {
          wx.showToast({
            title: res.data.text,
            icon: 'none',
            duration: 2000 //持续的时间
          })
        }
      })
    }
  },
  getList() {
    let timestamp = (Date.parse(new Date()) / 1000).toString();
    let _token =
      "userId=" +
      app.globalData.user_id +
      "&token=" +
      app.globalData.userToken +
      "&time=" +
      timestamp +
      "&roomId=" +
      this.data.roomId +
      "&page=" +
      this.data.page +
      "&wPQvCAOU$lbIyg9o2wVg0Dt1k^zy&SaX";
    let params = {
      userId: app.globalData.user_id,
      token: app.globalData.userToken,
      roomId: this.data.roomId,
      page: this.data.page,
      time: timestamp,
      sign: md5.hexMD5(_token),
    };
    axios.axios({
      url: "/chat/onlineUserList",
      data: params
    }).then((res) => {
      console.log(res.data.result),
        this.setData({
          onlineList: res.data.result
        })
    })
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    console.log(options)
    this.setData({
      roomId: options.roomId,
    })
    if (options.request) {
      this.setData({
        roomId: options.roomId,
        request: true,
      })
    } else {
      this.setData({
        roomId: options.roomId,
      })
    }
    this.getList()
  },

  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})